package phase01.module3.code.question2;

import java.util.Scanner;

/**
 * 编程获取两个指定字符串中的最大相同子串。
 * <p>
 * 如： s1="aasdafghjka", s2="aaasdfg" 他们的最大子串为"asd"
 * <p>
 * 提示： 将短的那个串进行长度依次递减的子串与较长的串比较。
 */
public class MaxSubString {
    public static void main(String[] args) {
        String s1;
        String s2;
        Scanner sc = new Scanner(System.in);
        System.out.print("输入两个字符串: ");
        s1 = sc.next();
        s2 = sc.next();
        int len1 = s1.length();
        int len2 = s2.length();
        String maxSub = "";
        if (len1 > len2) {
            for (int i = 0; i < len2; i++) {
                for (int j = len2; j > i; j--) {
                    String sub = s2.substring(i, j);
                    if (s1.contains(sub) && sub.length() > maxSub.length()) {
                        maxSub = sub;
                    }
                }
            }
        } else {
            for (int i = 0; i < len1; i++) {
                for (int j = len1; j > i; j--) {
                    String sub = s1.substring(i, j);
                    if (s2.contains(sub) && sub.length() > maxSub.length()) {
                        maxSub = sub;
                    }
                }
            }
        }
        System.out.println(maxSub);
    }
}
